<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>RocketMQ 消息发送机制详解</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            color: #333;
            line-height: 1.6;
        }
        .hero {
            background: linear-gradient(135deg, #6e8efb 0%, #a777e3 100%);
        }
        .section-title {
            position: relative;
            padding-bottom: 0.5rem;
        }
        .section-title:after {
            content: '';
            position: absolute;
            bottom: 0;
            left: 0;
            width: 60px;
            height: 3px;
            background: linear-gradient(90deg, #6e8efb 0%, #a777e3 100%);
        }
        .code-block {
            background: #2d2d2d;
            border-left: 4px solid #6e8efb;
        }
        .comparison-table {
            border-collapse: separate;
            border-spacing: 0;
            border-radius: 0.5rem;
            overflow: hidden;
        }
        .comparison-table th {
            background: #f8f9fa;
        }
        .comparison-table td, .comparison-table th {
            padding: 1rem;
            border: 1px solid #e9ecef;
        }
        .card {
            transition: all 0.3s ease;
            box-shadow: 0 2px 15px rgba(0,0,0,0.1);
        }
        .card:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 25px rgba(0,0,0,0.15);
        }
        .feature-icon {
            width: 50px;
            height: 50px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            margin-bottom: 1rem;
        }
        .footer-links a:hover {
            color: #a777e3;
            transition: color 0.2s ease;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero text-white py-20 px-4">
        <div class="container mx-auto max-w-5xl text-center">
            <h1 class="text-4xl md:text-5xl font-bold mb-6">RocketMQ 消息发送机制详解</h1>
            <p class="text-xl md:text-2xl mb-8 font-light max-w-3xl mx-auto">探索同步与异步消息发送的核心原理、实现机制及最佳实践</p>
            <div class="flex justify-center space-x-4">
                <div class="px-4 py-2 bg-white text-purple-600 rounded-full font-medium inline-flex items-center">
                    <i class="fas fa-rocket mr-2"></i>
                    <span>高性能</span>
                </div>
                <div class="px-4 py-2 bg-white bg-opacity-20 rounded-full font-medium inline-flex items-center">
                    <i class="fas fa-bolt mr-2"></i>
                    <span>低延迟</span>
                </div>
                <div class="px-4 py-2 bg-white bg-opacity-20 rounded-full font-medium inline-flex items-center">
                    <i class="fas fa-shield-alt mr-2"></i>
                    <span>高可靠</span>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <div class="container mx-auto max-w-5xl px-4 py-12">
        <!-- Introduction -->
        <section class="mb-16">
            <div class="bg-white rounded-xl p-8 shadow-md">
                <h2 class="text-2xl font-bold mb-4 text-gray-800">消息发送机制概述</h2>
                <p class="text-gray-600 mb-4">RocketMQ 提供了两种主要的消息发送方式：同步发送和异步发送，以满足不同业务场景的需求。理解这两种发送方式的差异和适用场景对于构建高性能、可靠的分布式系统至关重要。</p>
                
                <div class="mt-8 grid md:grid-cols-2 gap-8">
                    <div class="card bg-white p-6 rounded-lg">
                        <div class="feature-icon bg-blue-100 text-blue-600">
                            <i class="fas fa-sync-alt text-xl"></i>
                        </div>
                        <h3 class="text-xl font-bold mb-3 text-gray-800">同步发送</h3>
                        <p class="text-gray-600">发送消息后阻塞等待响应，确保消息可靠送达，适合对可靠性要求高的核心业务场景。</p>
                    </div>
                    <div class="card bg-white p-6 rounded-lg">
                        <div class="feature-icon bg-purple-100 text-purple-600">
                            <i class="fas fa-bolt text-xl"></i>
                        </div>
                        <h3 class="text-xl font-bold mb-3 text-gray-800">异步发送</h3>
                        <p class="text-gray-600">发送消息后立即返回，通过回调处理结果，适合高并发、低延迟要求的场景。</p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Sync Message Section -->
        <section class="mb-16">
            <div class="bg-white rounded-xl p-8 shadow-md">
                <h2 class="text-2xl font-bold mb-6 text-gray-800 section-title">同步发送消息的实现机制</h2>
                
                <h3 class="text-xl font-semibold mb-4 text-gray-700">同步发送的基本原理</h3>
                <p class="text-gray-600 mb-6">同步发送是指Producer发送消息后，线程会阻塞等待Broker的响应结果，确认消息是否成功存储，才会继续执行后续操作。</p>
                
                <div class="mb-8">
                    <h4 class="text-lg font-semibold mb-3 text-gray-700">同步发送的执行流程</h4>
                    <div class="mermaid mb-6">
                        graph TD
                            A[创建消息对象] --> B[获取Topic路由信息]
                            B --> C[构建发送请求包]
                            C --> D[发送请求到Broker]
                            D --> E[线程阻塞等待响应]
                            E -->|成功| F[返回SendResult]
                            E -->|失败| G[根据策略重试]
                            G -->|超过重试次数| H[抛出异常]
                    </div>
                    
                    <ul class="space-y-3 text-gray-600">
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <span><strong>发送前准备:</strong> 创建消息对象，设置Topic、Tag、Body等属性；获取Topic的路由信息，选择目标MessageQueue；构建发送请求包</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-exchange-alt text-blue-500 mt-1 mr-2"></i>
                            <span><strong>发送过程:</strong> 向选定的Broker发送消息请求；当前线程进入阻塞状态，等待Broker响应；Broker接收消息并存储后，返回响应结果</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-tasks text-purple-500 mt-1 mr-2"></i>
                            <span><strong>结果处理:</strong> 若收到成功响应，解除阻塞并返回SendResult；若超时或失败，根据重试策略进行重试；超过重试次数后，抛出异常</span>
                        </li>
                    </ul>
                </div>
                
                <div class="mb-8">
                    <h4 class="text-lg font-semibold mb-3 text-gray-700">核心代码示例</h4>
                    <div class="code-block rounded-lg p-4 text-gray-200 overflow-x-auto">
                        <pre><code>// 同步发送消息
Message msg = new Message("TopicTest", "TagA", "Hello RocketMQ".getBytes());
try {
    // 发送消息并等待结果返回
    SendResult sendResult = producer.send(msg);
    // 处理返回结果
    System.out.println("消息发送成功，消息ID: " + sendResult.getMsgId());
} catch (Exception e) {
    // 处理发送异常
    System.out.println("消息发送失败");
    e.printStackTrace();
}</code></pre>
                    </div>
                </div>
            </div>
        </section>

        <!-- Async Message Section -->
        <section class="mb-16">
            <div class="bg-white rounded-xl p-8 shadow-md">
                <h2 class="text-2xl font-bold mb-6 text-gray-800 section-title">异步发送消息的执行流程</h2>
                
                <h3 class="text-xl font-semibold mb-4 text-gray-700">异步发送的基本原理</h3>
                <p class="text-gray-600 mb-6">异步发送是指Producer发送消息后，不等待Broker的响应结果，立即返回继续执行后续逻辑。消息发送结果通过回调函数异步通知。</p>
                
                <div class="mb-8">
                    <h4 class="text-lg font-semibold mb-3 text-gray-700">异步发送的执行流程</h4>
                    <div class="mermaid mb-6">
                        graph TD
                            A[创建消息对象] --> B[获取Topic路由信息]
                            B --> C[注册回调函数]
                            C --> D[发送请求到Broker]
                            D --> E[立即返回]
                            E --> F[继续后续业务]
                            D -->|Broker响应| G[回调线程处理]
                            G -->|成功| H[调用onSuccess]
                            G -->|失败| I[调用onException]
                    </div>
                    
                    <ul class="space-y-3 text-gray-600">
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <span><strong>发送前准备:</strong> 创建消息对象，设置Topic、Tag、Body等属性；获取Topic的路由信息，选择目标MessageQueue；注册回调函数对象(SendCallback)</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-exchange-alt text-blue-500 mt-1 mr-2"></i>
                            <span><strong>发送过程:</strong> 向选定的Broker异步发送消息请求；立即返回，不阻塞当前线程；消息发送任务提交到内部线程池异步执行</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-tasks text-purple-500 mt-1 mr-2"></i>
                            <span><strong>结果处理:</strong> Broker响应后，Producer内部回调线程调用SendCallback；成功时触发onSuccess方法；失败时触发onException方法，并根据策略进行重试</span>
                        </li>
                    </ul>
                </div>
                
                <div class="mb-8">
                    <h4 class="text-lg font-semibold mb-3 text-gray-700">核心代码示例</h4>
                    <div class="code-block rounded-lg p-4 text-gray-200 overflow-x-auto">
                        <pre><code>// 异步发送消息
Message msg = new Message("TopicTest", "TagA", "Hello RocketMQ Async".getBytes());
// 注册回调接口
producer.send(msg, new SendCallback() {
    @Override
    public void onSuccess(SendResult sendResult) {
        // 处理发送成功逻辑
        System.out.println("消息发送成功，消息ID: " + sendResult.getMsgId());
    }
    
    @Override
    public void onException(Throwable e) {
        // 处理发送异常逻辑
        System.out.println("消息发送失败");
        e.printStackTrace();
    }
});

// 继续执行后续业务逻辑，不需等待消息发送结果
System.out.println("消息已提交异步发送");</code></pre>
                    </div>
                </div>
            </div>
        </section>

        <!-- Comparison Section -->
        <section class="mb-16">
            <div class="bg-white rounded-xl p-8 shadow-md">
                <h2 class="text-2xl font-bold mb-6 text-gray-800 section-title">两种发送方式的性能差异和适用场景</h2>
                
                <div class="mb-8">
                    <h3 class="text-xl font-semibold mb-4 text-gray-700">性能对比</h3>
                    <div class="overflow-x-auto">
                        <table class="comparison-table w-full">
                            <thead>
                                <tr>
                                    <th class="text-left">性能指标</th>
                                    <th class="text-center">同步发送</th>
                                    <th class="text-center">异步发送</th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr>
                                    <td class="font-medium">响应时间</td>
                                    <td class="text-center">较长(需等待Broker响应)</td>
                                    <td class="text-center">很短(立即返回)</td>
                                </tr>
                                <tr>
                                    <td class="font-medium">吞吐量</td>
                                    <td class="text-center">较低</td>
                                    <td class="text-center">较高</td>
                                </tr>
                                <tr>
                                    <td class="font-medium">资源消耗</td>
                                    <td class="text-center">较低(无需额外线程池)</td>
                                    <td class="text-center">较高(需维护发送线程池和回调线程池)</td>
                                </tr>
                                <tr>
                                    <td class="font-medium">CPU使用率</td>
                                    <td class="text-center">较低</td>
                                    <td class="text-center">较高</td>
                                </tr>
                                <tr>
                                    <td class="font-medium">内存占用</td>
                                    <td class="text-center">较低</td>
                                    <td class="text-center">较高(需缓存回调对象)</td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
                
                <div class="grid md:grid-cols-2 gap-8">
                    <div>
                        <h3 class="text-xl font-semibold mb-4 text-gray-700">同步发送适用场景</h3>
                        <ul class="space-y-3 text-gray-600">
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-blue-500 mt-1 mr-2"></i>
                                <span><strong>可靠性要求高的核心业务:</strong> 需要确保消息成功发送的场景，如订单创建、支付交易、账户金额变更等</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-blue-500 mt-1 mr-2"></i>
                                <span><strong>强一致性要求的业务流程:</strong> 业务流程中下一步依赖上一步的消息发送结果</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-blue-500 mt-1 mr-2"></i>
                                <span><strong>低频次调用场景:</strong> 消息发送频率不高，对性能要求不苛刻的场景</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-blue-500 mt-1 mr-2"></i>
                                <span><strong>问题排查简单性要求:</strong> 业务流程简单，易于调试和问题排查的场景</span>
                            </li>
                        </ul>
                    </div>
                    <div>
                        <h3 class="text-xl font-semibold mb-4 text-gray-700">异步发送适用场景</h3>
                        <ul class="space-y-3 text-gray-600">
                            <li class="flex items-start">
                                <i class="fas fa-bolt text-purple-500 mt-1 mr-2"></i>
                                <span><strong>高并发高吞吐量场景:</strong> 需要快速处理大量消息的场景，如日志收集、监控数据上报等</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-bolt text-purple-500 mt-1 mr-2"></i>
                                <span><strong>对响应时间敏感的场景:</strong> 用户交互型应用，需要快速响应</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-bolt text-purple-500 mt-1 mr-2"></i>
                                <span><strong>消息可靠性要求不是极高的场景:</strong> 允许小部分消息丢失的非核心业务</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-bolt text-purple-500 mt-1 mr-2"></i>
                                <span><strong>系统资源充足的场景:</strong> 服务器资源充足，可以支持额外的线程池开销</span>
                            </li>
                        </ul>
                    </div>
                </div>
                
                <div class="mt-8 bg-blue-50 p-6 rounded-lg">
                    <h3 class="text-xl font-semibold mb-4 text-gray-700">设计建议</h3>
                    <ul class="space-y-3 text-gray-600">
                        <li class="flex items-start">
                            <i class="fas fa-lightbulb text-yellow-500 mt-1 mr-2"></i>
                            <span><strong>针对不同业务需求选择合适的发送方式:</strong> 核心交易类业务使用同步发送，非核心高并发业务使用异步发送</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-lightbulb text-yellow-500 mt-1 mr-2"></i>
                            <span><strong>异步发送的注意事项:</strong> 正确配置发送线程池大小，避免资源耗尽；实现合理的异常处理和重试策略</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-lightbulb text-yellow-500 mt-1 mr-2"></i>
                            <span><strong>同步发送的优化:</strong> 合理设置超时时间和重试次数；避免在单线程中连续发送多条消息</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-lightbulb text-yellow-500 mt-1 mr-2"></i>
                            <span><strong>混合使用策略:</strong> 可在同一系统中针对不同业务特点，混合使用同步和异步发送</span>
                        </li>
                    </ul>
                </div>
            </div>
        </section>

        <!-- Conclusion -->
        <section class="mb-16 bg-gradient-to-r from-blue-50 to-purple-50 rounded-xl p-8 shadow-md">
            <h2 class="text-2xl font-bold mb-6 text-gray-800">总结</h2>
            <p class="text-gray-600 mb-4">RocketMQ通过提供多种消息发送方式，满足了不同业务场景的需求，开发人员应根据实际需求灵活选择，以获得最佳的系统性能和可靠性平衡。</p>
            <p class="text-gray-600">理解同步和异步发送的底层机制有助于我们在设计分布式系统时做出更明智的决策，构建出既高性能又可靠的应用程序。</p>
        </section>
    </div>

    <!-- Footer -->
    <footer class="bg-gray-900 text-white py-8">
        <div class="container mx-auto max-w-5xl px-4">
            <div class="flex flex-col md:flex-row justify-between items-center">
                <div class="mb-4 md:mb-0">
                    <h3 class="text-xl font-bold mb-2">技术小馆</h3>
                    <p class="text-gray-400">深入探索技术细节，分享实用解决方案</p>
                </div>
                <div class="footer-links">
                    <a href="http://www.yuque.com/jtostring" class="text-gray-300 hover:text-white transition-colors duration-200">
                        <i class="fas fa-globe mr-2"></i>技术小馆地址
                    </a>
                </div>
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'dark',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
    </script>
</body>
</html>